<?php

/**
 * ECSHOP 专题前台
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @author:     webboy <laupeng@163.com>
 * @version:    v2.1
 * ---------------------------------------------
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

if (empty($_REQUEST['act']))
{
	$_REQUEST['act'] = 'get';
}

if (empty($_SESSION['user_id']))
{
	ecs_header("Location: user.php?act=login\n");
}

$id = intval($_REQUEST['id']);
$bonus_type = bonus_type_info($id);
if ($bonus_type['type_id']<=0){
	show_message('该卡券不存在','返回首页','./','',false);
}
$time = gmtime();
if ($bonus_type['get_start_date'] > $time){
	show_message('该卡券尚未开放领取','返回首页','./','',false);
}
if ($bonus_type['get_end_date'] < $time){
	show_message('您已错过该卡券的领取时间','返回首页','./','',false);
}
if ($bonus_type['get_number'] >= $bonus_type['limit_number']){
	show_message('该卡券已被领取完毕','返回首页','./','',false);
}

if ($_REQUEST['act'] == 'get')
{
// 	print_r($bonus_type);
	$templates = 'bonus_get.dwt';
	$position = assign_ur_here();
	
	$smarty->assign('bonus_type',$bonus_type);
	$smarty->assign('page_title',       $position['title']);    // 页面标题
// 	$smarty->assign('ur_here',          $position['ur_here']);  // 当前位置
	$smarty->assign('keywords','');
	$smarty->assign('description','');
	$smarty->assign('helps',            get_shop_help());              // 网店帮助
	$smarty->assign('categories',       get_categories_tree_pro()); // 分类树
	assign_template();
	$smarty->assign('cfg', $_CFG);	
}
elseif ($_REQUEST['act'] == 'result')
{
	$templates = 'bonus_result.dwt';
// 	print_r($bonus_type);
	//限领
	$where = ' WHERE bonus_type_id = '.$id.' AND user_id = '.$_SESSION['user_id'];
	if($bonus_type['limit_type'] == 1){
		$dayStart = strtotime(date('Y-m-d',gmtime()));
		$dayEnd = strtotime(date('Y-m-d 23:59:59',gmtime()));
		$where .= ' AND add_time >= '.$dayStart.' AND add_time <= '.$dayEnd;
		$msg = '您今天已经领取过该卡券了';
	}else{
		$msg = '您已经领取过该卡券了';
	}
	$sql = 'SELECT 1 FROM '.$ecs->table('user_bonus').$where.' LIMIT 1';
	if ($db->getOne($sql)){
		show_message($msg,'返回首页','./','',false);
	}
	
	$sql = "INSERT INTO " . $ecs->table('user_bonus') .
	"(bonus_type_id, bonus_sn, user_id, used_time, order_id, emailed, sms, add_time) " .
	"VALUES ('$id', 0, '$_SESSION[user_id]', 0, 0, 0, 0, '".gmtime()."')";
	$db->query($sql);
	
	$smarty->assign('bonus_type',$bonus_type);
	//模板赋值
	$smarty->assign('cfg', $_CFG);
	assign_template();
	$position = assign_ur_here(0, $result['title']);
	$smarty->assign('page_title', $position['title']);    // 页面标题
// 	$smarty->assign('ur_here',    $position['ur_here']);  // 当前位置
	$smarty->assign('keywords',$result['title']);
	$smarty->assign('description','');
	$smarty->assign('categories',       get_categories_tree_pro()); // 分类树
	$smarty->assign('helps',      get_shop_help());       // 网店帮助
	
}

/* 显示模板 */
$smarty->display($templates);

/**
 * 取得红包类型信息
 * @param   int     $bonus_type_id  红包类型id
 * @return  array
 */
function bonus_type_info($bonus_type_id)
{
	$sql = "SELECT bt.*,COUNT(ub.bonus_id) AS get_number,c.cat_name FROM " . $GLOBALS['ecs']->table('bonus_type') . " AS bt ".
			"LEFT JOIN ".$GLOBALS['ecs']->table('user_bonus')." AS ub ON ub.bonus_type_id = bt.type_id ".
			" LEFT JOIN ".$GLOBALS['ecs']->table('category'). " AS c ON bt.cat_id = c.cat_id".
			" WHERE bt.type_id = '$bonus_type_id' AND bt.promotion_type = 1";
	$res = $GLOBALS['db']->getRow($sql);
	if ($res){
		$res['note'] = $res['type'] == BONUS_TYPE_DISCOUNT?'可打'.round($res['type_discount']/10,1).'折的优惠券':'价值为'.$res['type_money'].'元的代金券';
		$res['use_startdate']   = local_date('Y/m/d', $res['use_start_date']);
		$res['use_enddate']     = local_date('Y/m/d', $res['use_end_date']);
		$res['min_goods_amount'] = price_format($res['min_goods_amount'],false);
		if ($res['min_goods_amount'] > 0){
			$res['type_bonus'] = $res['money_type']==0?'满'.$res['min_goods_amount'].'元':'每满'.$res['min_goods_amount'].'元';
		}else{
			$res['type_bonus'] = '无';
		}
		
		$res['bonus'] = ($res['type']==BONUS_TYPE_DISCOUNT)?round($res['type_discount']/10,1).'折':price_format($res['type_money'],false).'元';
		$res['cat_name'] = ($res['cat_id']==0)?'全分类':$res['cat_name'].'及以下所有分类';
	}
	return $res;
}


?>